/**
 * Created by JetBrains WebStorm.
 * User: Silence
 * Date: 3/18/12
 * Time: 4:43 PM
 * To change this template use File | Settings | File Templates.
 */

Ext.define('EM.controller.event.EventPeopleViewController', {
    extend: 'Ext.app.Controller',
    models: ['person.Person'],
    views: ['event.EventPeopleView', 'event.AddPeopleView','event.AllPeopleView', 'event.SpecificView'],
    init: function() {
        var view = Ext.getCmp('addpeopleview');
        if (!view) view = Ext.widget('addpeopleview');
        view.hide();
        this.control({
            '#addpeopleviewbutton': {
                click: function() {
                    rowEditing.cancelEdit();
                    rowEditing2.cancelEdit();
                    view.show();
                }
            },
            '#removebutton': {
                click: this.doDelete
            },
            '#sendemailbutton': {
                click: this.sendEmail
            }
        });
    },
    sendEmail: function() {
        var emails = '';
        var grid = Ext.getCmp('eventpeopleview').getLayout().activeItem;
        var first = true;
        var selected = grid.getSelectionModel().getSelection();
        for (var i = 0; i < selected.length; i++) {
            if (first == false) emails += ';';
            first = false;
            emails += selected[i].data.email;
        }
        window.location = 'mailto:' + emails + '?body=\'Test content\'';
        //window.location = 'mailto:' + 'harta.wijaya.11.10@gmail.com' + '?body=\'Test content\'';
    },
    doDelete: function() {
        var num = Ext.getCmp('eventpeopleview').getLayout().activeItem.getSelectionModel().getSelection().length;
        Ext.MessageBox.confirm('Message', 'Do you really want to delete ' + num + ' people?' , this.doRealDelete);
    },
    doRealDelete: function(btn) {
        if (btn == 'yes') {
            var grid = Ext.getCmp('eventpeopleview').getLayout().activeItem;
            var store = grid.getStore();
            for (var select in grid.getSelectionModel().getSelection()) {
                //alert(select.get('id'));
                //select.id = -1;
            }
            store.remove(grid.getSelectionModel().getSelection());
        }
    }
});